import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import { join } from 'path';
export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1660399943751_1342';

  // add your egg config in here
  config.middleware = ['errorHandler']
  // 只对 /api 前缀的 url 路径生效
  config.errorHandler = {
    match: '/api',
  }


  config.view = {
    defaultViewEngine: 'nunjucks',
    mapping: {
      '.html': 'nunjucks'
    }
  }
console.log("join(__dirname, '../app/web')", join(__dirname, '../app/web'));
  config.assets={
    templatePath: join(appInfo.baseDir, 'app/view/index.html'),
    templateViewEngine: 'nunjucks',
    publicPath: '/public',
    devServer: {
      command: 'cross-env APP_ROOT=app/web max dev',
      debug: true,
      port: 8000,
      timeout: 180*1000,
      env: { // 设置环境变量，egg-view-assets 会和 process.env 合并
        // PATH: 'umi.cmd',
        // BROWSER: 'none',
        // ESLINT: 'none',
        // APP_ROOT: join(__dirname, '../app/web'),
        // SOCKET_SERVER: 'http://127.0.0.1:8000',
        // PUBLIC_PATH: 'http://127.0.0.1:8000',
      },
      headers: {
        "Access-Control-Allow-Origin": "*"
      },
    }
  }

  // config.cors = {
  //   origin: 'http://127.0.0.1:8000',//匹配规则  域名+端口  *则为全匹配
  //   allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  // };

  config.validate = {
    // convert: false,
    // validateRoot: false,
  }

  config.security = {
    csrf: false,
  };

  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };



  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
  };
};
